package cc.wanforme.fsync.config.restUpload;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传进度 保存中心
 */
public class UploadProgressCenter {

    /** 正在上传的队列。单个文件只能有一个上传线程，否则会有多线程竞争问题 */
    private static final Map<Long, SimpleProgressTracker> all = new HashMap<>(3);


    public static SimpleProgressTracker getOrDefault(Long id, long fileSize) {
        return all.computeIfAbsent(id, (key) -> new SimpleProgressTracker(fileSize));
    }

    /** 文件上传结束 */
    public static void closeTracker(Long id) {
        SimpleProgressTracker tracker = all.remove(id);
        if(tracker != null) {
            tracker.finished();
        }
    }

    public static Map<Long, SimpleProgressTracker> getAll() {
        return all;
    }


}
